package io.gitee.yaoshanli.engine.service.impl;

import io.gitee.yaoshanli.engine.db.JwDbHelper;
import io.gitee.yaoshanli.engine.service.ITaskHandlerService;
import io.gitee.yaoshanli.engine.utils.StringUtils;
import io.gitee.yaoshanli.engine.model.WfTaskHandlerModel;

import java.util.ArrayList;
import java.util.List;

public class TaskHandlerService implements ITaskHandlerService {

    @Override
    public boolean addUser(String wf_id, String task_id, String... users)
    {
        String sql="insert into jw_wf_task_handler (id,wf_id,task_id,user_id) values (?,?,?,?)";
        Object[][] params = new Object[users.length][];
        for (int i = 0; i < users.length; i++) {
            String user = users[i];
            params[i] = new Object[]{StringUtils.GUID(),wf_id,task_id,user};
        }
        return JwDbHelper.batch(sql,params);
    }

    @Override
    public boolean removeUser(String wf_id, String task_id, String... user) {
        String sql="delete from jw_wf_task_handler where wf_id=? and task_id=? and user=?";
        Object[] params=new Object[]{wf_id,task_id,user};
        return JwDbHelper.update(sql,params)>0;
    }

    @Override
    public boolean removeAllUser(String wf_id, String task_id) {
        String sql="delete from jw_wf_task_handler where wf_id=? and task_id=?";
        Object[] params=new Object[]{wf_id,task_id};
        return JwDbHelper.update(sql,params)>0;
    }

    @Override
    public List<WfTaskHandlerModel> getUserList(String wf_id, String task_id) {
        Object[] params=new Object[]{wf_id,task_id};
        String sql="select * from jw_wf_task_handler where wf_id=? and task_id=? ";
        List<WfTaskHandlerModel> list=JwDbHelper.queryList(WfTaskHandlerModel.class,sql,params);
        return list;
    }

    @Override
    public List<WfTaskHandlerModel> handlerUser(String wf_id, String task_id, String user)
    {
        String sql="delete from jw_wf_task_handler where wf_id=? and task_id=? and user=?";
        Object[] params=new Object[]{wf_id,task_id,user};
        if(JwDbHelper.update(sql,params)>0)
        {
            params=new Object[]{wf_id,task_id};
            sql="select * from jw_wf_task_handler where wf_id=? and task_id=? ";
            List<WfTaskHandlerModel> list=JwDbHelper.queryList(WfTaskHandlerModel.class,sql,params);
            return list;
        }
        return new ArrayList<>();
    }
}
